using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Sample.Outlook.CRMIntegration.ItemForms
{
	internal sealed partial class ActivityAssociationsControl : UserControl
	{
		private Guid? _accountID;
		private Guid _activityID;

		public ActivityAssociationsControl(Guid? accountID, Guid activityID)
		{
			InitializeComponent();
			_accountID = accountID;
			_activityID = activityID;
		}

		Dictionary<int, Guid> _mapIndexToContactID = new Dictionary<int, Guid>();
		Dictionary<int, Guid> _mapIndexToOpportunityID = new Dictionary<int, Guid>();

		private void AssociationsControl_Load(object sender, EventArgs e)
		{
			LoadActivityAssociations();
		}

		private void LoadActivityAssociations()
		{
			listContacts.Items.Clear();
			_mapIndexToContactID.Clear();
			listOpportunities.Items.Clear();
			_mapIndexToOpportunityID.Clear();

			if (!_accountID.HasValue)
				return;

			contactAssociationsTableAdapter.FillByActivity(associationsDataSet.ContactAssociations, _activityID, _accountID.Value);
			foreach (AssociationsDataSet.ContactAssociationsRow row in associationsDataSet.ContactAssociations)
			{
				int i = listContacts.Items.Add(
					string.Format("{0} {1}", row.FirstName, row.LastName),
					row.IsAssociated);
				_mapIndexToContactID.Add(i, row.LocalID);
			}

			opportunityAssociationsTableAdapter.FillByActivity(associationsDataSet.OpportunityAssociations, _activityID, _accountID.Value);
			foreach (AssociationsDataSet.OpportunityAssociationsRow row in associationsDataSet.OpportunityAssociations)
			{
				int i = listOpportunities.Items.Add(row.Name, row.IsAssociated);
				_mapIndexToOpportunityID.Add(i, row.LocalID);
			}
		}

		public void ChangeAccount(Guid accountID)
		{
			_accountID = accountID;
			LoadActivityAssociations();
		}

		public void Save()
		{
			List<Guid> checkedContacts = new List<Guid>();
			foreach (int i in listContacts.CheckedIndices)
				checkedContacts.Add(_mapIndexToContactID[i]);

			UpdateContacts(checkedContacts);


			List<Guid> checkedOpportunities = new List<Guid>();
			foreach (int i in listOpportunities.CheckedIndices)
				checkedOpportunities.Add(_mapIndexToOpportunityID[i]);

			UpdateOpportunities(checkedOpportunities);
		}

		private void UpdateContacts(List<Guid> checkedContacts)
		{
			CrmDataSetTableAdapters.ActivityContactsTableAdapter contactsAdapter = new CrmDataSetTableAdapters.ActivityContactsTableAdapter();
			CrmDataSet.ActivityContactsDataTable contactsTable = new CrmDataSet.ActivityContactsDataTable();
			contactsAdapter.FillByActivityID(contactsTable, _activityID);

			foreach (CrmDataSet.ActivityContactsRow row in contactsTable)
			{
				if (checkedContacts.Contains(row.LocalID) == false)
					row.Delete();
				else
					checkedContacts.Remove(row.LocalID);
			}

			foreach (Guid contactID in checkedContacts)
			{
				CrmDataSet.ActivityContactsRow row = contactsTable.NewActivityContactsRow();
				row.LocalID = Guid.NewGuid();
				row.ActivityID = _activityID;
				row.ContactID = contactID;

				contactsTable.AddActivityContactsRow(row);
			}

			contactsAdapter.Update(contactsTable);
		}

		private void UpdateOpportunities(List<Guid> checkedOpportunities)
		{
			CrmDataSetTableAdapters.OpportunityActivityTableAdapter opportunitiesAdapter = new CrmDataSetTableAdapters.OpportunityActivityTableAdapter();
			CrmDataSet.OpportunityActivityDataTable opportunitiesTable = new CrmDataSet.OpportunityActivityDataTable();
			opportunitiesAdapter.FillByActivityID(opportunitiesTable, _activityID);

			foreach (CrmDataSet.OpportunityActivityRow row in opportunitiesTable)
			{
				if (checkedOpportunities.Contains(row.LocalID) == false)
					row.Delete();
				else
					checkedOpportunities.Remove(row.LocalID);
			}

			foreach (Guid opportunityID in checkedOpportunities)
			{
				CrmDataSet.OpportunityActivityRow row = opportunitiesTable.NewOpportunityActivityRow();
				row.LocalID = Guid.NewGuid();
				row.ActivityID = _activityID;
				row.OpportunityID = opportunityID;

				opportunitiesTable.AddOpportunityActivityRow(row);
			}

			opportunitiesAdapter.Update(opportunitiesTable);
		}
	}
}
